/*! ******************************************************************************
 *
 * Created on 2021年9月25日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通用日志工具类,提供了debug, info, warn, error四个级别日志的通用访问途径。
 *
 * @author hs@yzbdl.ac.cn
 */
public class LogUtil {

    private static final Logger DEBUG_LOG = LoggerFactory.getLogger("Lanius.debug");
    private static final Logger INFO_LOG = LoggerFactory.getLogger("Lanius.info");
    private static final Logger WARN_LOG = LoggerFactory.getLogger("Lanius.warn");
    private static final Logger ERROR_LOG = LoggerFactory.getLogger("Lanius.error");

    private static String getStackTrack() {
        StackTraceElement ste = Thread.currentThread().getStackTrace()[3];
        return ste.getClassName() + "." + ste.getMethodName() + "()-->Line:" + ste.getLineNumber() + "-->";
    }

    public static void debug(String message) {
        if (LogUtil.DEBUG_LOG.isDebugEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.DEBUG_LOG.debug(message);
        }
    }

    public static void debug(String message, Object... args) {
        if (LogUtil.DEBUG_LOG.isDebugEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.DEBUG_LOG.debug(message, args);
        }
    }

    public static void debug(String message, Throwable e) {
        if (LogUtil.DEBUG_LOG.isDebugEnabled()) {
            message = LogUtil.getStackTrack() + message;
            if (e != null) {
                LogUtil.DEBUG_LOG.debug(message, e);
            } else {
                LogUtil.DEBUG_LOG.debug(message);
            }
        }
    }

    public static void info(String message) {
        if (LogUtil.INFO_LOG.isInfoEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.INFO_LOG.info(message);
        }
    }

    public static void info(String message, Object... args) {
        if (LogUtil.INFO_LOG.isInfoEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.INFO_LOG.info(message, args);
        }
    }

    public static void info(String message, Throwable e) {
        if (LogUtil.INFO_LOG.isInfoEnabled()) {
            message = LogUtil.getStackTrack() + message;
            if (e != null) {
                LogUtil.INFO_LOG.info(message, e);
            } else {
                LogUtil.INFO_LOG.info(message);
            }
        }
    }

    public static void warn(String message) {
        if (LogUtil.WARN_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.WARN_LOG.error(message);
        }
    }

    public static void warn(String message, Object... args) {
        if (LogUtil.WARN_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.WARN_LOG.error(message, args);
        }
    }

    public static void warn(String message, Throwable e) {
        if (LogUtil.WARN_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            if (e != null) {
                LogUtil.WARN_LOG.error(message, e);
            } else {
                LogUtil.WARN_LOG.error(message);
            }
        }
    }

    public static void error(String message) {
        if (LogUtil.ERROR_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.ERROR_LOG.error(message);
        }
    }

    public static void error(String message, Object... args) {
        if (LogUtil.ERROR_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            LogUtil.ERROR_LOG.error(message, args);
        }
    }

    public static void error(String message, Throwable e) {
        if (LogUtil.ERROR_LOG.isErrorEnabled()) {
            message = LogUtil.getStackTrack() + message;
            if (e != null) {
                LogUtil.ERROR_LOG.error(message, e);
            } else {
                LogUtil.ERROR_LOG.error(message);
            }
        }
    }

}
